<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 缺少treeNode -->
        <TreeNode :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <TreeNode
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @deptEdit="deptEdit"
            @addDepts="addDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 放置新增弹层组件  -->
    <addDept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import TreeNode from './components/tree-tools.vue'
import { getDepartmentsAPI } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
import addDept from './components/add-dept.vue'
export default {
  components: {
    TreeNode,
    addDept
  },
  props: {
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      company: {},
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      // 定义变量控制弹层组件的显示和隐藏
      showDialog: false,
      node: null
    }
  },
  computed: {},
  mounted() {
    this.getDepartments()
  },
  methods: {
    // 获取数据
    async getDepartments() {
      const result = await getDepartmentsAPI()
      // console.log(result.depts)
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = transListToTreeData(result.depts, '')
      // this.departs = result.depts // 需要将其转化成树形结构
    },
    // 添加操作
    addDepts(node) {
      // console.log(node)
      this.showDialog = true
      this.node = node
    },
    // 编辑操作
    deptEdit(node) {
      this.showDialog = true
      this.node = node
      this.$refs.addDept.getDepartDetail(node.id)
      console.log(node.id)
    }
  }
}
</script>
<style scoped>
/* .tree-card {
  padding: 30px  140px;
  font-size:14px;
} */
</style>
